﻿using RabbitMQ.Client;
using System;
using System.Collections.Generic;
using System.Text;

namespace Logistics.Commn.RabbitMQ
{
    public class RabbitMQHelper
    {
        private static ConnectionFactory factory;
        private static object lockObj = new object();

        /// <summary>
        /// 获取单个RabbitMQ连接
        /// </summary>
        /// <returns></returns>
        public static IConnection GetConnection()
        {
            if (factory == null)
            {
                lock (lockObj)
                {
                    if (factory == null)
                    {
                        factory = new ConnectionFactory
                        {
                            HostName = "10.31.64.11", //ip
                            Port = 5672, //端口
                            UserName = "lcy", //账号
                            Password = "123456", //密码
                            VirtualHost = "virtualHost1" //虚拟主机
                        };

                        //设置端口后自动恢复连接属性
                        factory.AutomaticRecoveryEnabled = true;
                    }
                }
            }
            return factory.CreateConnection();
        }
    }
}